﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolProject2012.Areas.Pedagogie.Models;
using SchoolProject2012.Functions;

namespace SchoolProject2012.Areas.Pedagogie.Controllers
{
    [Authorize(Roles = "Admin, Pedagogie")]
    public class EleveController : Controller
    {
        private SchoolProjectBddEntities1 db = new SchoolProjectBddEntities1();

        //
        // GET: /Pedagogie/Eleve/

        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            var eleve = db.ELEVE.Include("CHARGEDEPLACEMENT");
            return View(eleve.ToList());
        }

        //
        // GET: /Pedagogie/Eleve/Details/5

        public ActionResult Details(string id = null)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            if (eleve == null)
            {
                return HttpNotFound();
            }
            return View(eleve);
        }

        //
        // GET: /Pedagogie/Eleve/Create

        public ActionResult Create()
        {
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE");
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS");
            return View();
        }

        //
        // POST: /Pedagogie/Eleve/Create

        [HttpPost]
        public ActionResult Create(ELEVE eleve)
        {       
           
            if (ModelState.IsValid)
            {
                eleve.MDPPERS = Security.getHash(eleve.MDPPERS);
                eleve.LOGINPERS = Login.generateLogin(eleve.NOMPERS, eleve.PRENOMPERS);
                db.ELEVE.AddObject(eleve);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.ELEVECLASS.Single(x => x.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS);
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            return View(eleve);
        }

        //
        // GET: /Pedagogie/Eleve/Edit/5

        public ActionResult Edit(string id = null)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            if (eleve == null)
            {
                return HttpNotFound();
            }
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.ELEVECLASS.Single(x => x.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS);
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            return View(eleve);
        }

        //
        // POST: /Pedagogie/Eleve/Edit/5

        [HttpPost]
        public ActionResult Edit(ELEVE eleve)
        {
            if (ModelState.IsValid)
            {
                eleve.MDPPERS = Security.getHash(eleve.MDPPERS);
                eleve.LOGINPERS = Login.generateLogin(eleve.NOMPERS, eleve.PRENOMPERS);
                db.ELEVE.Attach(eleve);
                db.ObjectStateManager.ChangeObjectState(eleve, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.IDCLASSE = new SelectList(db.CLASSE, "IDCLASSE", "LIBCLASSE", eleve.ELEVECLASS.Single(x => x.CLASSE.ANNEECLASSE == Utils.getCurrentYearForClass()).IDCLASS);
            ViewBag.CHA_LOGINPERS = new SelectList(db.CHARGEDEPLACEMENT, "LOGINPERS", "NOMPERS", eleve.CHA_LOGINPERS);
            return View(eleve);
        }

        //
        // GET: /Pedagogie/Eleve/Delete/5

        public ActionResult Delete(string id = null)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            if (eleve == null)
            {
                return HttpNotFound();
            }
            return View(eleve);
        }

        //
        // POST: /Pedagogie/Eleve/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {
            ELEVE eleve = db.ELEVE.Single(e => e.LOGINPERS == id);
            db.ELEVE.DeleteObject(eleve);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Tuiles()
        {
            return View();
        }
    }
}